﻿using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;

// Toolkit namespace
using SimpleMvvmToolkit;

namespace ToolRental.Client.SMvvm.Admin
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvmprop</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// </summary>
    public class DevicesViewModel : ViewModelBase<DevicesViewModel>, INotifyError
    {
        private IDeviceServiceAgent serviceAgent;

        #region Constructor

        public DevicesViewModel() { }

        public    DevicesViewModel(IDeviceServiceAgent serviceAgent)
        {
            this.serviceAgent = serviceAgent;
            RefreshData();
            SelectedDeviceTypeIndex = 0;
        }

        #endregion

        #region NotifyError
        public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;

        
        // Helper method to notify View of an error
        private void NotifyError(string message, Exception error)
        {
            // Notify view of an error
            Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
        }
        #endregion

        #region ToolTypeSelectionChanged
        public async void ToolTypeSelectionChanged()
        {
            if (SelectedDeviceType != null)
            {
                var devices = await serviceAgent.GetDeviceList(SelectedDeviceType.Id, this);
                DeviceList = new ObservableCollection<Device>(devices);
            }
        }
        #endregion

        #region RefreshData
        public async void RefreshData()
        {
            var deviceTypes = await serviceAgent.GetDeviceTypeList(this);
            DeviceTypeList = new ObservableCollection<DeviceType>(deviceTypes);
            ToolTypeSelectionChanged();
        }
        #endregion

        #region AddNewDevice
        public void AddNewDevice()
        {
            if (NewToolStockNumber != "")
            {
                if (SelectedDeviceType != null)
                {
                    serviceAgent.AddNewDevice(NewToolStockNumber, SelectedDeviceType.Id, this);
                    NewToolStockNumber = "";
                    ToolTypeSelectionChanged();
                }
                else
                {
                    ErrorOccured("Kérem válassza ki az eszköz típusát!", null);
                }
            }
            else
            {
                ErrorOccured("Kérem adja meg az eszköz raktári számát!", null);
            }
        }
        #endregion

        #region DeviceList
        private ObservableCollection<Device> deviceList;
        public ObservableCollection<Device> DeviceList
        {
            get { return deviceList; }
            set
            {
                deviceList = value;
                NotifyPropertyChanged(dl => dl.DeviceList);
            }
        }
        #endregion

        #region SelectedDeviceIndex
        private int selectedDeviceIndex;
        public int SelectedDeviceIndex
        {
            get { return selectedDeviceIndex; }
            set
            {
                selectedDeviceIndex = value;
                NotifyPropertyChanged(sd => sd.SelectedDeviceIndex);
            }
        }
        #endregion

        #region SelectedDevice
        private Device selectedDevice;
        public Device SelectedDevice
        {
            get { return selectedDevice; }
            set
            {
                selectedDevice = value;
                NotifyPropertyChanged(sd => sd.SelectedDevice);
            }
        }
        #endregion

        #region DeviceTypeList
        private ObservableCollection<DeviceType> deviceTypeList;
        public ObservableCollection<DeviceType> DeviceTypeList
        {
            get { return deviceTypeList; }
            set
            {
                deviceTypeList = value;
                NotifyPropertyChanged(dtl => dtl.DeviceTypeList);
            }
        }
        #endregion

        #region SelectedDeviceType
        private DeviceType selectedDeviceType;
        public DeviceType SelectedDeviceType
        {
            get { return selectedDeviceType; }
            set
            {
                selectedDeviceType = value;
                NotifyPropertyChanged(sdt => sdt.SelectedDeviceType);
            }
        }
        #endregion

        #region SelectedDeviceTypeIndex
        private int selectedDeviceTypeIndex;
        public int SelectedDeviceTypeIndex
        {
            get { return selectedDeviceTypeIndex; }
            set
            {
                selectedDeviceTypeIndex = value;
                NotifyPropertyChanged(m => m.SelectedDeviceTypeIndex);
            }
        }
        #endregion

        #region NewToolStockNumber
        private string newToolStockNumber;
        public string NewToolStockNumber
        {
            get { return newToolStockNumber; }
            set
            {
                newToolStockNumber = value;
                NotifyPropertyChanged(sn => sn.NewToolStockNumber);
            }
        }
        #endregion

        #region ErrorOccured
        public void ErrorOccured(string message, Exception exception)
        {
            MessageBox.Show(message);
        }
        #endregion
    }
}